import 'package:flutter/material.dart';
import 'package:xsdk_flutter/xsdk/framework/base/xsdk_widget_stateful.dart';
import 'package:xsdk_flutter/xsdk/framework/component/select_address/bean/x_bean_address.dart';
import 'package:xsdk_flutter/xsdk/utils/x_utils_color.dart';
import 'package:xsdk_flutter/xsdk/utils/x_utils_flutter.dart';
import 'package:xsdk_flutter/xsdk/utils/x_utils_string.dart';
import 'package:xsdk_flutter/xsdk/utils/x_utils_style.dart';
import 'package:xsdk_flutter/xsdk/utils/x_utils_widget.dart';


class XSelectAddressState extends XState{


  List<XBeanAddressInfoData> addressInfoDataList;

  double cupertinoPickerDefaultHeight1=300;
  double cupertinoPickerItemHeight=48;


  //当前选中结果
  late XBeanSelectAddress selectAddressResult;


  XSelectAddressState(
      {
        required this.addressInfoDataList,
        Map<String,dynamic>? selectAddressResultMap,
      }
      ){
    if(selectAddressResultMap!=null){
      selectAddressResult=XBeanSelectAddress.fromJson(selectAddressResultMap);
    }else{
      selectAddressResult=XBeanSelectAddress();
    }
  }

  FixedExtentScrollController? scrollControllerProvince;
  FixedExtentScrollController? scrollControllerCity;
  FixedExtentScrollController? scrollControllerRegion;

  @override
  void onCreate() {
  }


  @override
  Widget onBuild(BuildContext context) {

    //titlebar
    Widget titleBarWidget=_getTitleBarWidget();
    //波轮
    Widget itemWidget=_getItemWidget();

    //底部弹框是圆角的,同其一致
    return XWidgetUtils.getXLayoutRoundedRectWithBorder(
        height: 360,
        width: double.infinity,
        backgroundColor: Colors.white,
        borderColor: null,
        radius_topLeft: 15,
        radius_topRight: 15,
        radius_bottomLeft: 0,
        radius_bottomRight: 0,
        child: XWidgetUtils.getLayoutColumn(
            children: [
              titleBarWidget,
              itemWidget,
            ]
        )
    );
  }



  @override
  void onDestroy() {
  }

  @override
  void onStart() {
  }


  @override
  Future<void> updateData({int pageIndex = 1}) async {
  }


  ////////////////////////////////////////////////////

  XBeanAddressInfoData? _currentSelectProvince;
  XBeanAddressInfoData? _currentSelectCity;
  XBeanAddressInfoData? _currentSelectRegion;

  Widget _getItemWidget(){

    int scrollerInitIndexProvince=0;
    int scrollerInitIndexCity=0;
    int scrollerInitIndexRegion=0;
    _currentSelectProvince=null;
    _currentSelectCity=null;
    _currentSelectRegion=null;

    //setState更新UI时重新定位到当前

    //省
    int sizeProvince=addressInfoDataList.length;
    if(sizeProvince!=0){
      if(!XStringUtils.isEmpty(selectAddressResult.provinceName)){
        for(int i=0;i<sizeProvince;i++){
          XBeanAddressInfoData province=addressInfoDataList[i];
          if(province.name==selectAddressResult.provinceName){
            scrollerInitIndexProvince=i;
            _currentSelectProvince=province;
            break;
          }
        }
      }

      _currentSelectProvince ??= addressInfoDataList[0];

      //市
      if(_currentSelectProvince!.children!=null){

        int sizeCity=_currentSelectProvince!.children!.length;

        if(sizeCity!=0){

          if(!XStringUtils.isEmpty(selectAddressResult.cityName)){
            for(int j=0;j<sizeCity;j++){
              XBeanAddressInfoData city=_currentSelectProvince!.children![j];
              if(city.name==selectAddressResult.cityName){
                scrollerInitIndexCity=j;
                _currentSelectCity=city;
                break;
              }
            }
          }
          _currentSelectCity??=_currentSelectProvince!.children![0];

          //区
          if(_currentSelectCity!.children!=null){
            int sizeRegion=_currentSelectCity!.children!.length;
            if(sizeRegion!=0){
              if(!XStringUtils.isEmpty(selectAddressResult.regionName)){
                for(int k=0;k<sizeRegion;k++){
                  XBeanAddressInfoData region=_currentSelectCity!.children![k];
                  if(region.name==selectAddressResult.regionName){
                    scrollerInitIndexRegion=k;
                    _currentSelectRegion=region;
                    break;
                  }
                }
              }
              _currentSelectRegion??=_currentSelectCity!.children![0];
            }
          }

        }
      }

      //
      selectAddressResult.provinceId=_currentSelectProvince?.id;
      selectAddressResult.provinceName=_currentSelectProvince?.name;
      selectAddressResult.cityId=_currentSelectCity?.id;
      selectAddressResult.cityName=_currentSelectCity?.name;
      selectAddressResult.regionId=_currentSelectRegion?.id;
      selectAddressResult.regionName=_currentSelectRegion?.name;

    }

    scrollControllerProvince=FixedExtentScrollController(initialItem:scrollerInitIndexProvince);
    scrollControllerCity=FixedExtentScrollController(initialItem:scrollerInitIndexCity);
    scrollControllerRegion=FixedExtentScrollController(initialItem:scrollerInitIndexRegion);

    //拨轮
    Widget cupertinoPickerWidget= XWidgetUtils.getLayoutRow(
        mainAxisAlignment:MainAxisAlignment.center,
        children: [
          XWidgetUtils.getLayoutExpanded(
              XWidgetUtils.getWidgetCupertinoPicker(
                  scrollController: scrollControllerProvince,
                  height: cupertinoPickerDefaultHeight1,
                  itemHeight:cupertinoPickerItemHeight,
                  data: addressInfoDataList,
                  onSelectedItemChanged: (int index){
                    //省拨轮
                    setState(() {
                      //省
                      _currentSelectProvince=addressInfoDataList[index];
                      _currentSelectCity=null;
                      _currentSelectRegion=null;

                      //市
                      if(_currentSelectProvince!.children!=null && _currentSelectProvince!.children!.isNotEmpty){
                        _currentSelectCity=_currentSelectProvince!.children![0];
                        //区
                        if(_currentSelectCity!.children!=null && _currentSelectCity!.children!.isNotEmpty){
                          _currentSelectRegion=_currentSelectCity!.children![0];
                        }
                      }

                      //
                      selectAddressResult.provinceId=_currentSelectProvince?.id;
                      selectAddressResult.provinceName=_currentSelectProvince?.name;
                      selectAddressResult.cityId=_currentSelectCity?.id;
                      selectAddressResult.cityName=_currentSelectCity?.name;
                      selectAddressResult.regionId=_currentSelectRegion?.id;
                      selectAddressResult.regionName=_currentSelectRegion?.name;

                    });
                  },
                  itemWidgetBuilder: (dynamic value){
                    String txt='${(value as XBeanAddressInfoData).name}';
                    return XWidgetUtils.getWidgetText(txt,style: XStyleUtils.textStyle_000_18(),maxLines: 1,isChineseWordAddCode:false);
                  }
              )
          ),
          (_currentSelectProvince?.children==null || _currentSelectProvince?.children?.length==0)?
          XWidgetUtils.getLayoutContainer():XWidgetUtils.getLayoutExpanded(
              XWidgetUtils.getWidgetCupertinoPicker(
                  scrollController: scrollControllerCity,
                  height: cupertinoPickerDefaultHeight1,
                  itemHeight:cupertinoPickerItemHeight,
                  data: _currentSelectProvince!.children!,
                  onSelectedItemChanged: (int index){
                    //市拨轮
                    setState(() {
                      //市
                      _currentSelectCity=_currentSelectProvince!.children![index];
                      _currentSelectRegion=null;

                      //区
                      if(_currentSelectCity!.children!=null && _currentSelectCity!.children!.isNotEmpty){
                        _currentSelectRegion=_currentSelectCity!.children![0];
                      }

                      //
                      selectAddressResult.provinceId=_currentSelectProvince?.id;
                      selectAddressResult.provinceName=_currentSelectProvince?.name;
                      selectAddressResult.cityId=_currentSelectCity?.id;
                      selectAddressResult.cityName=_currentSelectCity?.name;
                      selectAddressResult.regionId=_currentSelectRegion?.id;
                      selectAddressResult.regionName=_currentSelectRegion?.name;

                    });

                  },
                  itemWidgetBuilder: (dynamic value){
                    String txt='${(value as XBeanAddressInfoData).name}';
                    return XWidgetUtils.getWidgetText(txt,style: XStyleUtils.textStyle_000_18(),maxLines: 1,isChineseWordAddCode:false);
                  }
              )
          ),
          (_currentSelectCity?.children==null || _currentSelectCity?.children?.length==0)?
          XWidgetUtils.getLayoutContainer():XWidgetUtils.getLayoutExpanded(
              XWidgetUtils.getWidgetCupertinoPicker(
                  scrollController: scrollControllerRegion,
                  height: cupertinoPickerDefaultHeight1,
                  itemHeight:cupertinoPickerItemHeight,
                  data: _currentSelectCity!.children!,
                  onSelectedItemChanged: (int index){
                    //区拨轮

                    setState(() {
                      //市
                      _currentSelectRegion=_currentSelectCity!.children![index];

                      //
                      selectAddressResult.provinceId=_currentSelectProvince?.id;
                      selectAddressResult.provinceName=_currentSelectProvince?.name;
                      selectAddressResult.cityId=_currentSelectCity?.id;
                      selectAddressResult.cityName=_currentSelectCity?.name;
                      selectAddressResult.regionId=_currentSelectRegion?.id;
                      selectAddressResult.regionName=_currentSelectRegion?.name;

                    });

                  },
                  itemWidgetBuilder: (dynamic value){
                    String txt='${(value as XBeanAddressInfoData).name}';
                    return XWidgetUtils.getWidgetText(txt,style: XStyleUtils.textStyle_000_18(),maxLines: 1,isChineseWordAddCode:false);
                  }
              )
          ),
        ]
    );

    return XWidgetUtils.getLayoutContainer(
        child: cupertinoPickerWidget
    );


  }




  //ok
  Widget _getTitleBarWidget(){
    return XWidgetUtils.getLayoutContainer(
      height: 48,
      margin: XWidgetUtils.getEdgeInsets(0, 0, 0, 5),
      padding: XWidgetUtils.getEdgeInsets(5, 0, 5, 0),
      child: XWidgetUtils.getLayoutRow(
          children: [
            XWidgetUtils.getWidgetButtonText(
                text: '取消',
                buttonStyle: XStyleUtils.getButtonStyle(fontColor: XColorUtils.color666666,fontSize: 14),
                onPressed: (){
                  XFlutterUtils.finishPage(context);
                }
            ),
            XWidgetUtils.getLayoutExpanded(
                XWidgetUtils.getLayoutCenter(XWidgetUtils.getWidgetText('选择地址',style: XStyleUtils.textStyle_000_16()))
            ),
            XWidgetUtils.getWidgetButtonText(
                text: '确定',
                buttonStyle: XStyleUtils.getButtonStyle(fontColor: XColorUtils.appMainColor,fontSize: 14),
                onPressed: (){
                  //返回选中结果

                  XFlutterUtils.finishPage(context);
                  if(widget.resultCallback!=null){
                    widget.resultCallback!(selectAddressResult.toJson());
                  }

                }
            ),
          ]
      ),
    );
  }



}